package org.shj.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.ParseException;
import java.util.Date;

/**
 * @author Shen Huang Jian
 * @since 2018-06-22
 */

public class DateUtils extends org.apache.commons.lang3.time.DateUtils {

    public static String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static String DATETIME_FORMAT = "yyyyMMddHHmmss";
    public static String DATE_FORMAT = "yyyy-MM-dd";

    public static Date parse(String dateStr) {
        if (StringUtils.isEmpty(dateStr)) {
            return null;
        }
        try {
            return DateUtils.parseDate(dateStr, DEFAULT_FORMAT);
        } catch (ParseException pe) {
            return null;
        }
    }

    public static Date parse(String dateStr, String parsePatterns) {
        if (StringUtils.isEmpty(dateStr) || StringUtils.isEmpty(parsePatterns)) {
            return null;
        }
        try {
            return DateUtils.parseDate(dateStr, parsePatterns);
        } catch (ParseException pe) {
            return null;
        }
    }



    public static String formatDateTime(Date date){
        if(date == null){
            return "";
        }
        return DateFormatUtils.format(date, DEFAULT_FORMAT);
    }

    public static String formatDateTime(Date date, String format){
        return DateFormatUtils.format(date, format);
    }

}

